package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.entity.biz.ArticleSearch;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.entity.core.Regular;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

public class TiebaSearchScheme extends BasicCustomScheme{
	public List<ArticleSearch> findTitleInfo(Page page, Entry entry) {
		List<ArticleSearch> articleList = new ArrayList<>();
		//----------------------------------爬取搜索页面的标题，发布时间，url
		List<String> partList = page.getHtml().xpath("//div[@class='s_post']").all();//待爬取的新闻列表
		for(int i=0;i<partList.size();i++){
			Html childHtml = Html.create(partList.get(i));//将找到的div字符串转为html
			ArticleSearch temp = new ArticleSearch();
			temp.setTitle(childHtml.xpath("//span[@class='p_title']/a/text()").get());
			temp.setNewsSource(childHtml.xpath("//a[@class='p_forum']/allText()").get());
			temp.setPubTimeStr(childHtml.xpath("//font[@class='p_green p_date']/text()").get());
			temp.setAuthor(childHtml.xpath("/html/body/div/a[2]/font/text()").get());
			String authorUrl ="http://tieba.baidu.com"+ childHtml.xpath("//a[2]").links().get();
			temp.setAuthorUrl(authorUrl);
			String url ="http://tieba.baidu.com"+ childHtml.xpath("//span[@class='p_title']/a").links().get();
			temp.setUrl(url);
			if(!"".equals(url)&&url!=null){
				articleList.add(temp);
			}
		}
		return articleList;
	}

	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();// 存放分页list
		boolean flag = false;
		String nextUrl = page.getHtml().$("a[class=next]","href").toString();
		String prevUrl = page.getHtml().$("a[class=pre]","href").toString();
		if (!"".equals(nextUrl)&&nextUrl!=null) {//抓【下一页】链接
			pgList.add("http://tieba.baidu.com"+nextUrl);
		}
		if ((!"".equals(prevUrl)&&prevUrl!=null)||(!"".equals(nextUrl)&&nextUrl!=null)) {//包含上一页或者下一页链接
			flag = true;
		}
		if (!flag) {//没有上一页链接也没有下一页链接 ，取全部
			pgList = page.getHtml().xpath("//div[@class='pager pager-search']").links().all();// 存放分页list
			
		}
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(pgList);
		return resultSet;
	}
	
	@Override
	public void preParse(Page page, Configuration configuration) {
		List<Regular> regulars = new ArrayList<Regular>();
		//回帖内容1
		Regular regular = new Regular();
		regular.setEntryId(configuration.getEntry().getEntryId());
		String url = page.getUrl().get();
		String id = StringUtils.substringAfter(url, "#");
		regular.setSelect("//div[@id='post_content_"+id+"']/text()");
		regular.setAttr("text");
		regular.setRegulartype("content");
		regular.setSelectorType(1);
		regular.setFieldtype("String");
		regulars.add(regular);
		
		//回复数
		Regular regular2 = new Regular();
		regular2.setEntryId(configuration.getEntry().getEntryId());
		regular2.setSelect("//*[@id='thread_theme_5']/div[1]/ul/li[2]/span[1]/text()");
		regular2.setAttr("text");
		regular2.setRegulartype("commentcount");
		regular2.setSelectorType(1);
		regular2.setFieldtype("Integer");
		regulars.add(regular2);
		
		//回帖内容2
		Regular regular3 = new Regular();
		regular3.setEntryId(configuration.getEntry().getEntryId());
		String url3 = page.getUrl().get();
		String idstr = StringUtils.substringAfter(url3, "pid=");
		String id3 = StringUtils.substringBefore(idstr, "&cid");
		regular3.setSelect("//div[@id='post_content_"+id3+"']/text()");
		regular3.setAttr("text");
		regular3.setRegulartype("content");
		regular3.setSelectorType(1);
		regular3.setFieldtype("String");
		regulars.add(regular3);
		configuration.getEntry().setRegulars(regulars);;
		
	}
}
